package com.realbrand.net.realbrandpay.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

import com.realbrand.net.realbrandpay.R;
import com.realbrand.net.realbrandpay.bean.AdressBean;

import java.util.List;
import java.util.zip.Inflater;

/**
 * Created by Administrator on 2018/5/14.
 */

public class AdressAdapter extends BaseAdapter {
    private List<AdressBean> data;
    private Context mContext;
    private LayoutInflater mInflater;
    //设置接口
    private View.OnClickListener bt_setDefault;
    private View.OnClickListener bt_compile;
    private View.OnClickListener bt_delete;

    public AdressAdapter(List<AdressBean> data, Context mContext) {
        this.data = data;
        this.mContext = mContext;
        mInflater = LayoutInflater.from(mContext);
    }

    //设置接口方法
    public void onSetDefault(View.OnClickListener setDefault) {
        this.bt_setDefault = setDefault;
    }

    public void onCompile(View.OnClickListener compile) {
        this.bt_compile = compile;
    }

    public void onDelete(View.OnClickListener delete) {
        this.bt_delete = delete;
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ManagerAdressViewHelder managerAdressViewHelder = null;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.adressitem_layout, parent, false);
            managerAdressViewHelder = new ManagerAdressViewHelder(convertView);
            managerAdressViewHelder.managerAdress_lv_item_bt_compile.setOnClickListener(bt_compile);
            managerAdressViewHelder.managerAdress_lv_item_bt_delete.setOnClickListener(bt_delete);
            managerAdressViewHelder.managerAdress_lv_item_bt_setDefault.setOnClickListener(bt_setDefault);
            convertView.setTag(managerAdressViewHelder);
        } else {
            managerAdressViewHelder = (ManagerAdressViewHelder) convertView.getTag();
        }
        managerAdressViewHelder.managerAdress_lv_item_tv_User.setText(data.get(position).getAddresseeName().get(position));
        managerAdressViewHelder.managerAdress_lv_item_tv_Phone.setText(data.get(position).getTelephone().get(position));
        managerAdressViewHelder.managerAdress_lv_item_tv_Adress.setText(data.get(position).getProvince().get(position) +
                data.get(position).getRegionCity().get(position) + data.get(position).getCountyDistrict().get(position));
        managerAdressViewHelder.managerAdress_lv_item_bt_setDefault.setTag(position);
        managerAdressViewHelder.managerAdress_lv_item_bt_delete.setTag(position);
        managerAdressViewHelder.managerAdress_lv_item_bt_compile.setTag(position);
        if (data.get(position).getAddressType().get(position).equals("0")) {
            managerAdressViewHelder.managerAdress_lv_item_bt_setDefault.setBackground(mContext.getResources().getDrawable(R.drawable.ic_enough_o));
            managerAdressViewHelder.managerAdress_lv_item_tv_defalut.setText("默认地址");
        } else {
            managerAdressViewHelder.managerAdress_lv_item_bt_setDefault.setBackground(mContext.getResources().getDrawable(R.drawable.yuan_hook));
            managerAdressViewHelder.managerAdress_lv_item_tv_defalut.setText("设为默认");
        }
        return convertView;
    }

    class ManagerAdressViewHelder {
        TextView managerAdress_lv_item_tv_User;
        TextView managerAdress_lv_item_tv_Phone;
        TextView managerAdress_lv_item_tv_Adress;
        Button managerAdress_lv_item_bt_setDefault;
        Button managerAdress_lv_item_bt_compile;
        Button managerAdress_lv_item_bt_delete;
        TextView managerAdress_lv_item_tv_defalut;

        public ManagerAdressViewHelder(View view) {
            managerAdress_lv_item_tv_User = (TextView) view.findViewById(R.id.managerAdress_lv_item_tv_user);
            managerAdress_lv_item_tv_Phone = (TextView) view.findViewById(R.id.managerAdress_lv_item_tv_phone);
            managerAdress_lv_item_tv_Adress = (TextView) view.findViewById(R.id.managerAdress_lv_item_tv_adress);
            managerAdress_lv_item_bt_setDefault = (Button) view.findViewById(R.id.managerAdress_lv_item_bt_setDefault);
            managerAdress_lv_item_bt_delete = (Button) view.findViewById(R.id.managerAdress_lv_item_bt_delete);
            managerAdress_lv_item_bt_compile = (Button) view.findViewById(R.id.managerAdress_lv_item_bt_compile);
            managerAdress_lv_item_tv_defalut = (TextView) view.findViewById(R.id.managerAdress_lv_item_tv_default);
        }
    }
}
